from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader, RequestContext
from booksite.models import BookInfo, HeroInfo

# Create your views here.
def my_render(request, template_path, context_dict={}):
    temp = loader.get_template(template_path)
    context = RequestContext(request, context_dict)
    res_html = temp.render(context)
    return HttpResponse(res_html)

def index(request):
    return render(request, 'booksite/index.html', {'content':'hello world', 'list':list(range(1,10))})
    # return my_render(request, 'booksite/index.html')
    # return HttpResponse('老铁，六六六')
    # temp = loader.get_template('booksite/index.html')
    # context = RequestContext(request, {})
    # res_html = temp.render(context)
    # return HttpResponse(res_html)


def index2(request):
    return HttpResponse('何必忧伤')

def show_books(request):
    books = BookInfo.objects.all()
    return render(request, 'booksite/show_books.html', {'books':books})
    # books = BookInfo.objects.all()

def detail(request, bid):
    '''查询图书对应的英雄信息'''
    book = BookInfo.objects.get(id=bid)
    heros = book.heroinfo_set.all()
    return render(request, 'booksite/detail.html', {'book':book, 'heros':heros})